package com.xinqizhi.controller;


import com.common.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinqizhi.entity.XqzClassList;
import com.xinqizhi.service.XqzClassListService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**  
 * @ClassName: XqzClassListController  
 * @Description: ${todo}(控制器)  
 * @author yangtongquan  
 * @date 2019-07-08  
 *  
 */
@Controller
@RequestMapping("/class_list")
public class XqzClassListController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private XqzClassListService xqzClassListService;

	@RequestMapping("/index")
	public String toClassManageIndex(){
		return "redirect:../views/class-list.jsp";
	}

	/**
	* 查询一条数据
	* @param clsID
	* @return
	*/
    @RequestMapping("/data")
	@ResponseBody
     public Map getData(Integer clsID) {
		Map<String, Object> result = new HashMap<>(16);
		try {
            XqzClassList XqzClassList = xqzClassListService.getData(clsID);
			//result = CommonUtils.getUpdateAndAddResult(result, XqzClassList != null, METHOD_TYPE.SELECT);
			result.put("success",true);
			result.put("msg","根据clsID查询信息成功");
			result.put("data", XqzClassList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			//result = CommonUtils.getErrorResult(result, e, METHOD_TYPE.SELECT);
			result.put("success",false);
			result.put("msg","查询数据失败"+ StringUtils.substring(e.getMessage(),0,100));
			result.put("data",null);
		}
		return result;
     }

	/**
	* 分页查询数据列表
	* @param XqzClassList
	* @return
	*/
	@RequestMapping("/list")
	@ResponseBody
	public Map getList(XqzClassList XqzClassList) {
	Map<String, Object> result = new HashMap<>(16);
		try {
			String filterName = XqzClassList.getFiltername();
			if (XqzClassList.getPaging()){
			PageHelper.startPage(XqzClassList.getPage(),XqzClassList.getLimit());
			}
			List<XqzClassList> xqzClassList = xqzClassListService.getAllData(XqzClassList);
			PageInfo page = new PageInfo(xqzClassList);
			//result = CommonUtils.getUpdateAndAddResult(result, true, METHOD_TYPE.SELECT);
			result.put("count", page.getTotal());
			result.put("data", page.getList());
			result.put("code",0);
			result.put("msg","查询班级列表成功");
			result.put("success",true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			//result = CommonUtils.getErrorResult(result, e, METHOD_TYPE.SELECT);
		}
		return result;
	}

    /**
    * 插入一条数据
    * @param XqzClassList
    * @return
    */
    @RequestMapping("/add")
	@ResponseBody
    public Map add(XqzClassList XqzClassList) {
    Map<String, Object> result = new HashMap<>(16);

		try {
	        XqzClassList.setClassCreateDt(new Timestamp(System.currentTimeMillis()));
			int flag =  xqzClassListService.insertSelectData(XqzClassList);
			//result = CommonUtils.getUpdateAndAddResult(result, flag == 1, METHOD_TYPE.ADD);
			result.put("success",true);
			result.put("msg","添加班级信息成功");
			/*result.put("data", XqzClassList);*/
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			//result = CommonUtils.getErrorResult(result, e, METHOD_TYPE.ADD);
			result.put("success", false);
			result.put("msg","添加数据发生异常"+StringUtils.substring(e.getMessage(),0,100));
		}
		return result;
    }

    /**
    * 更新数据
    * @param XqzClassList
    * @return
    */
    @RequestMapping("/update")
	@ResponseBody
    public Map update(XqzClassList XqzClassList) {
    Map<String, Object> result = new HashMap<>(16);
		try {
			XqzClassList.setClassCreateDt(new Timestamp(System.currentTimeMillis()));
			int flag = xqzClassListService.updateData(XqzClassList);
			result.put("success",true);
			result.put("msg","修改班级信息成功");
			//result = CommonUtils.getUpdateAndAddResult(result, flag > 0, METHOD_TYPE.UPDATE);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			//result = CommonUtils.getErrorResult(result, e, METHOD_TYPE.UPDATE);
			result.put("success",false);
			result.put("msg","修改班级信息失败err:"+StringUtils.substring(e.getMessage(),0,100));
		}
		return result;
    }

    /**
    * 删除数据
    * @param clsID
    * @return
    */
    @RequestMapping("/delete")
	@ResponseBody
    public Map delete(Integer clsID){
    Map<String, Object> result = new HashMap<>(16);
    result.put("success", false);
		try {
			int flag = xqzClassListService.deleteData(clsID);
			//result = CommonUtils.getUpdateAndAddResult(result, flag > 0, METHOD_TYPE.DELETE);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			//result = CommonUtils.getErrorResult(result, e, METHOD_TYPE.DELETE);
		}
		return result;
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @RequestMapping("/batchDelete")
	@ResponseBody
    public Map batchDelete(String ids){
    Map<String, Object> result = new HashMap<>(16);
    result.put("success", false);
		try {
			int flag = xqzClassListService.deleteBatchData(ids.split(","));
			//result = CommonUtils.getUpdateAndAddResult(result, flag > 0, METHOD_TYPE.BATCH_DEL);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			//result = CommonUtils.getErrorResult(result, e, METHOD_TYPE.BATCH_DEL);
		}
		return result;
    }
}
